package com.company.oa.workflow.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.common.api.Result;
import com.company.oa.workflow.dto.ApprovalDTO;
import com.company.oa.workflow.entity.ApprovalRecord;
import com.company.oa.workflow.service.ApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/workflow/approval")
public class ApprovalController {
    
    @Autowired
    private ApprovalService approvalService;
    
    /**
     * 获取待办任务
     */
    @GetMapping("/todo")
    public Result<IPage<Map<String, Object>>> getTodoTasks(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String businessType) {
        // 从当前登录用户中获取用户ID
        Long userId = 1L; // 实际项目中应从SecurityContext中获取
        IPage<Map<String, Object>> pageResult = approvalService.getTodoTasks(page, size, userId, businessType);
        return Result.success(pageResult);
    }
    
    /**
     * 获取已办任务
     */
    @GetMapping("/done")
    public Result<IPage<ApprovalRecord>> getDoneTasks(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String businessType) {
        // 从当前登录用户中获取用户ID
        Long userId = 1L; // 实际项目中应从SecurityContext中获取
        IPage<ApprovalRecord> pageResult = approvalService.getDoneTasks(page, size, userId, businessType);
        return Result.success(pageResult);
    }
    
    /**
     * 审批任务
     */
    @PostMapping("/approve")
    @PreAuthorize("hasAuthority('workflow:approval:approve')")
    public Result<Boolean> approveTask(@RequestBody @Valid ApprovalDTO approvalDTO) {
        // 从当前登录用户中获取用户ID
        Long userId = 1L; // 实际项目中应从SecurityContext中获取
        boolean success = approvalService.approveTask(approvalDTO, userId);
        return Result.success(success);
    }
    
    /**
     * 获取审批记录
     */
    @GetMapping("/records")
    public Result<IPage<ApprovalRecord>> getApprovalRecords(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam String processInstanceId) {
        IPage<ApprovalRecord> pageResult = approvalService.getApprovalRecords(page, size, processInstanceId);
        return Result.success(pageResult);
    }
    
    /**
     * 获取流程图
     */
    @GetMapping("/diagram")
    public Result<String> getProcessDiagram(@RequestParam String processInstanceId) {
        String diagramUrl = approvalService.getProcessDiagram(processInstanceId);
        return Result.success(diagramUrl);
    }
    
    /**
     * 获取任务详情
     */
    @GetMapping("/task/{taskId}")
    public Result<Map<String, Object>> getTaskDetail(@PathVariable String taskId) {
        Map<String, Object> detail = approvalService.getTaskDetail(taskId);
        return Result.success(detail);
    }
}